<?php

declare(strict_types=1);

namespace WanZ\App\User\GrpcService;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use WanZ\App\User\Grpc\User\UserServiceInterface;
use WanZ\App\User\Grpc\User\UserResponseEntity;
use WanZ\App\User\Grpc\User\UserBindPhoneRequest;
use WanZ\App\User\Grpc\User\UserBindPhoneResponse;
use WanZ\App\User\Grpc\User\UserShowRequest;
use WanZ\App\User\Grpc\User\UserShowResponse;
use WanZ\App\User\Logic\UserLogic;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;

class UserGrpc implements UserServiceInterface
{
    /**
     * @Inject
     * @var UserLogic
     */
    private $userLogic;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    private $logger;  

    public function show(UserShowRequest $request)
    {
        ## 请求参数
        $userId = (int) $request->getUId();
        $companyId = (int) $request->getCompanyId();
		$groupId = (int) $request->getGroupId() ?: 0;
        $isHiddenPhone = (int) $request->getIsHiddenPhone() ?: 0;

        $response = new UserShowResponse();
        try {
            $userInfo = $this->userLogic->getUserInfoById($userId, $companyId, $isHiddenPhone, $groupId);
            
            $userData = new UserResponseEntity();
            $userData->setId($request->getUId());
            $userData->setTitle('response1: ==> ' . $request->getUId());  //$userInfo
            $userData->setIcon('response2: ==> ' . $request->getUId());
            $userData->setStock($request->getCompanyId());
            $userData->setCreatedAt('response3: ==> ' . $request->getCompanyId());
            $userData->setUpdatedAt('response4: ==> ' . $request->getCompanyId());

            $response->setCode(200)
                     ->setMsg('Success')
                     ->setData($userData);

        } catch (\Throwable $e) {
            $this->logger->error(sprintf('%s [%s] %s', '获取用户信息失败', date('Y-m-d H:i:s'), $e->getMessage()));
            $this->logger->error($e->getTraceAsString());

            $response->setCode(ErrorCode::SERVER_ERROR)->setMsg('Failure')->setData('获取用户信息失败');
        }

		return $response;
    }

    public function bindPhone(UserBindPhoneRequest $request)
    {
        ## 请求参数
        $userType = (int) $request->getUType();
        $phone = (string) $request->getPhone();
        $code = (string) $request->getCode();
        $userId = (int) $request->getUId();
        $type = (string) $request->getType();

        $response = new UserBindPhoneResponse();
        try {        
            $result = $this->userLogic->bind($phone, $code, $userId, $type);
            if (! $result) {
                throw new CommonException(ErrorCode::SERVER_ERROR, '绑定手机号失败');
            }

            $response->setCode(200)
                    ->setMsg('Success')
                    ->setData('');

        } catch (\Throwable $e) {
            $this->logger->error(sprintf('%s [%s] %s', '绑定手机号失败', date('Y-m-d H:i:s'), $e->getMessage()));
            $this->logger->error($e->getTraceAsString());

            $response->setCode(ErrorCode::SERVER_ERROR)->setMsg('Failure')->setData('绑定手机号失败');
        }

		return $response; 
    }

}
